package com.nb.hdj.util;

import com.alibaba.fastjson.JSON;
import com.nb.hdj.model.MultimediaManagement;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 获取yaml文件转换为List
 * @Author: hedongjia
 * @Date: 2021/11/11 下午10:06
 * @Version: v1.0
 */
public class YamlToObject{
    MultimediaManagement multimediaManagement=new MultimediaManagement();
    private static List<Map<String, String>> getYamlList(String pageYaml) {
        List<Map<String, String>> list = new ArrayList();
        Map<String, Map<String, String>> map = readYamlUtil(pageYaml);
        for (Map.Entry<String, Map<String, String>> me : map.entrySet()) {
            Map<String, String> numNameMapValue = me.getValue();
            Map<String, String> tmp = new HashMap<>();
            for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) {
                String numKey = nameMapEntry.getKey();
                String nameValue = nameMapEntry.getValue();
                tmp.put(numKey, nameValue);
            }
            list.add(tmp);
        }
        return list;
    }

    /**
     * 通过Yaml的key 返回map列表
     * @param pageYaml
     * @return
     */
    private static List<Map<String, String>> getYamlListByKey(String pageYaml,String key) {
        List<Map<String, String>> keyList = new ArrayList();
        Map<String, Map<String, String>> map = readYamlUtil(pageYaml);
        for (Map.Entry<String, Map<String, String>> me : map.entrySet()) {
            //判断yaml的主key是否包括传递的key值
            if(me.getKey().contains(key)) {
                Map<String, String> numNameMapValue = me.getValue();
                Map<String, String> tmp = new HashMap<>();
                for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) {
                    String numKey = nameMapEntry.getKey();
                    String nameValue = nameMapEntry.getValue();
                    tmp.put(numKey, nameValue);
                }
                keyList.add(tmp);
            }
        }
        return keyList;
    }

    public static Map<String, Map<String, String>> readYamlUtil(String pageYaml) {
        Map<String, Map<String, String>> map = null;
        try {
            Yaml yaml = new Yaml();
            URL url = YamlToObject.class.getClassLoader().getResource("com/nb/hdj/page/"+pageYaml);
            //System.out.println(url);
            if (url != null) {
                //获取yaml文件中的配置数据，然后转换为Map
                map = yaml.load(new FileInputStream(url.getFile()));
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;

    }

    @DataProvider
    public Object[][] loginYamlErrorDataMethod() {
        List<Map<String, String>> yamlList = getYamlList("logindata-error.yaml");
        Object[][] files = new Object[yamlList.size()][];
        //System.out.println(files);
        for (int i = 0; i < yamlList.size(); i++) {
            files[i] = new Object[]{yamlList.get(i)};
            //System.out.println(files[i]);
        }
        //System.out.println(files);
        return files;
    }

    @DataProvider
    public Object[][] ConsultYamlInsertDataMethod() {
        List<Map<String, String>> yamlList = getYamlList("consultdata-insert.yaml");
        Object[][] files = new Object[yamlList.size()][];
        //System.out.println(files);
        for (int i = 0; i < yamlList.size(); i++) {
            files[i] = new Object[]{yamlList.get(i)};
            //System.out.println(files[i]);
        }
        //System.out.println(files);
        return files;
    }

    @DataProvider
    public Object[][] ConsultYamlSelectDataMethod() {
        List<Map<String, String>> yamlList = getYamlListByKey("consultdata-select.yaml","Select");
        Object[][] files = new Object[yamlList.size()][];
        //System.out.println(files);
        for (int i = 0; i < yamlList.size(); i++) {
            files[i] = new Object[]{yamlList.get(i)};
            //System.out.println(files[i]);
        }
        //System.out.println(files);
        return files;
    }

    @DataProvider
    public  Object[][] getMultimediaData(){
        multimediaManagement.setCourseCoverAddress("/Users/dongjia/Desktop/理顾宝一期后台.png")
                .setCourseName("测试课程")
                .setCourseIntroduce("介绍")
                .setCourseContent("测试内容")
                .setNote("假装备注");
        return  new  Object[][]{
                {multimediaManagement}
        };
    }

}
